package com.zimmur.controller;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.mysql.fabric.xmlrpc.base.Array;
import com.zimmur.common.AppModuleEnum;
import com.zimmur.common.IDWorkFactory;
import com.zimmur.common.ReturnJsonEntity;
import com.zimmur.common.StatusCodeEnum;
import com.zimmur.model.account.GoodsUser;
import com.zimmur.model.account.User;
import com.zimmur.model.account.view.ConcernView;
import com.zimmur.model.goods.Goods;
import com.zimmur.model.zone.Topic;
import com.zimmur.model.zone.TopicCollect;
import com.zimmur.model.zone.TopicComment;
import com.zimmur.model.zone.TopicExample;
import com.zimmur.model.zone.TopicFav;
import com.zimmur.model.zone.TopicGoods;
import com.zimmur.model.zone.TopicNotice;
import com.zimmur.model.zone.TopicPraise;
import com.zimmur.model.zone.view.ConcernUserView;
import com.zimmur.service.IAccountInfoService;
import com.zimmur.service.IUserService;
import com.zimmur.service.account.IPointLogService;
import com.zimmur.service.designer.IDesignerInfoService;
import com.zimmur.service.goods.IGoodsService;
import com.zimmur.service.zone.ITopicCollectService;
import com.zimmur.service.zone.ITopicCommentPraiseService;
import com.zimmur.service.zone.ITopicCommentService;
import com.zimmur.service.zone.ITopicConcernService;
import com.zimmur.service.zone.ITopicFavService;
import com.zimmur.service.zone.ITopicGoodsService;
import com.zimmur.service.zone.ITopicNoticeService;
import com.zimmur.service.zone.ITopicPictureService;
import com.zimmur.service.zone.ITopicPraiseService;
import com.zimmur.service.zone.ITopicRecommandService;
import com.zimmur.service.zone.ITopicService;
import com.zimmur.tools.DateUtils;
import com.zimmur.tools.PageUtil;
import com.zimmur.tools.PageUtil_TopicCollect;

@RestController  
@RequestMapping("/restful/topic")  
// /
public class TopicController extends BaseController {  
	private static Logger log=LoggerFactory.getLogger(TopicController.class);
	 @Resource  
	 private ITopicService topicService;    
	 @Resource  
	 private ITopicPictureService topicPictureService;   
	 @Resource  
	 private ITopicCollectService topicCollectService;
	 @Resource  
	 private ITopicConcernService topicConcernService;
	 @Resource  
	 private ITopicPraiseService topicPraiseService;
	 @Resource  
	 private ITopicCommentService topicCommentService;
	 @Resource  
	 private ITopicGoodsService topicGoodsService;
	 @Resource  
	 private ITopicNoticeService topicNoticeService;
	 @Resource  
	 private IUserService userService;
	 @Resource  
	 private IAccountInfoService accountInfoService;
	 @Resource  
	 private IGoodsService goodsService;
	 @Resource
     private IPointLogService pointService;
	 @Resource
	 private ITopicCommentPraiseService topicCommentPraiseService;
	 @Resource
     private ITopicRecommandService topicRecommandService;
	 @Resource
	 private ITopicFavService topicFavService;
	 @Resource
	 private IDesignerInfoService designerInfoService;
	 
	 @Value("#{spingMvcProperties['System.GuestId']}")
	 private String guessUserId;
	 
	 /**
	 * 社区公告明细  (app2.0已删除)
	 * @param request
	 * @param model
	 * @return
	 */
	 @ResponseBody
     @RequestMapping(value="/topicnoticedetail",method=RequestMethod.GET)  
     public ReturnJsonEntity topicNoticeDetail(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum=StatusCodeEnum.CODE100000;
		try{
			long noticeId = getLongParam("noticeId");
	
			TopicNotice notice =topicNoticeService.selectByNoticeIdTopicNotice(noticeId);
			
			entity.setData(notice);    
			statusCodeEnum = StatusCodeEnum.CODE000000;
		} catch (Exception e) {
			log.error(e.getMessage());
			statusCodeEnum=StatusCodeEnum.CODE100000;
			entity.setData(new Array());
		}
	    entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
     }
	 /**
	 * 社区公告(app2.0已删除)
	 * @param request
	 * @param model
	 * @return
	 */
	 @ResponseBody
     @RequestMapping(value="/topicnoticeList10",method=RequestMethod.GET)  
     public ReturnJsonEntity oneTopicNotice(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum=StatusCodeEnum.CODE100000;
		try{
	//		long noticeId = getLongParam("noticeId");
	
			List<TopicNotice> noticeList =topicNoticeService.selectTopicNoticeListLimit(10);
			
			entity.setData(noticeList);  
			statusCodeEnum = StatusCodeEnum.CODE000000;
		} catch (Exception e) {
			log.error(e.getMessage());
			statusCodeEnum=StatusCodeEnum.CODE100000;
			entity.setData(new Array());
		}
	    entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
     }
	 /**
	 * 话题查询
	 * @param request
	 * @param model
	 * @return
	 */
	 @ResponseBody
     @RequestMapping(value="/topicquery",method=RequestMethod.POST)  
     public ReturnJsonEntity TopicQuery(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum=StatusCodeEnum.CODE100000;
		try{
			Integer pageNow = getIntParam("pageNow");
			String key = getParam("key");
			Long userId = getTokenUserId();
//			PageUtil<Topic> pageList =topicService.topicQuery(key,userId,pageNow);
			PageUtil<com.zimmur.solr.model.Topic> pageList =topicService.topicQueryBySolr(key,userId,pageNow);
			
			entity.setData(pageList); 
			statusCodeEnum = StatusCodeEnum.CODE000000;
		} catch (Exception e) {
			log.error(e.getMessage());
			statusCodeEnum=StatusCodeEnum.CODE100000;
			entity.setData(new Array());
		}
	    entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
     }
 	 /**
	 * 社区主页
	 * @param request
	 * @param model
	 * @return
	 */
	 @ResponseBody
     @RequestMapping(value="/topicindex",method=RequestMethod.GET)  
     public ReturnJsonEntity topicIndex(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum=StatusCodeEnum.CODE100000;
		try{
	        long createUserId= getTokenUserId();
	        TopicExample topicExamplet = topicService.topicIndex(createUserId);
	
	        entity.setData(topicExamplet);
	        statusCodeEnum = StatusCodeEnum.CODE000000;
		} catch (Exception e) {
			log.error(e.getMessage());
			statusCodeEnum=StatusCodeEnum.CODE100000;
			entity.setData(new Array());
		}
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
     }
	 /**
	 * 首页全部 话题 分页
	 * @param request
	 * @param model
	 * @return
	 */
	 @ResponseBody
     @RequestMapping(value="/topicIndexpage",method=RequestMethod.GET)  
     public ReturnJsonEntity topicIndexPage(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum=StatusCodeEnum.CODE100000;
		try{
	        Integer pageNow = getIntParam("pageNow");
	        Integer type=getIntParam("type");
	        long createUserId =getTokenUserId();
	        PageUtil<Topic> pageList;
	        if(type==1 || type == 2 || type ==3){
	        	pageList=topicService.topicPage(createUserId, pageNow,type.shortValue());
	        }else{
	        	pageList=new PageUtil<>();
	        }
	        
	
	        entity.setData(pageList);
	        statusCodeEnum = StatusCodeEnum.CODE000000;
		} catch (Exception e) {
			log.error(e.getMessage());
			statusCodeEnum=StatusCodeEnum.CODE100000;
			entity.setData(new Array());
		}
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
     }	 
	 /**
	 * 发起话题 （星客，知客）
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value="/addtopic",method=RequestMethod.POST)  
    public ReturnJsonEntity addtopic(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum = StatusCodeEnum.CODE100000;
		//游客身份验证
		if(String.valueOf(getTokenUserId()).equals(guessUserId)){
			statusCodeEnum=StatusCodeEnum.CODE100200;
			entity.setCode(statusCodeEnum.getCode());
			entity.setMsg(statusCodeEnum.getMsg());
			return entity;
		}
		//获取当前时间，转为INT存储
        Integer timeDate =DateUtils.getUnixTimeByDate(DateUtils.getNow());
//        String categoryCode = getParam("categoryCode");
        String title = getParam("title");
        String content = getParam("content");        
        String pictures = getParam("pictures");//图片，备注
        String goods = getParam("goods");//推荐商品
        
        Double reward = getDoubleParam("reward");
        Long createUser=getTokenUserId();
        
	    Topic topic =new Topic();
	    topic.setTitle(title);
        topic.setContent(content);
        topic.setCreateDate(timeDate);
        topic.setCreateUser(createUser);
        topic.setTopicId(IDWorkFactory.getID(AppModuleEnum.Zone));//不使用自增，自己创建ID
        topic.setTopicType((short)3);
        Integer resut=0;
        
        try {
        	//根据用户Id取得用户信息
        	User user = userService.getUserInfoByUserId(createUser);
        	if(user!=null){
        		topic.setAuthor(user.getNickName());
        	}
        	
		    if("xk".equals(user.getCategoryCode())){
		    	resut=topicService.insertTopicAndStar(topic);
		    	if(resut > 0){
			    	//星客存储，推荐商品    //
			    	String[] sArr=goods.split(",");
			    	if(sArr[0] != "" && sArr.length > 0){
				    	TopicGoods topicGoods=new TopicGoods();
				    	for(String s:sArr){		    	
					    	topicGoods.setCreateDate(timeDate);
					    	topicGoods.setTopicId(topic.getTopicId());
					    	topicGoods.setUserId(createUser);
					    	long goodsId=Long.parseLong(s);
					    	topicGoods.setGoodsId(goodsId);
					    	Integer resutTopicRecommand = topicGoodsService.insertTopicGoods(topicGoods);
					    	//每一次话题推荐商品就为该商品加1此推荐数
					    	if(resutTopicRecommand > 0){
						    	Goods goodsS = goodsService.selectByGoodsId(goodsId);
								if(goodsS != null){
									//社区推荐数
									Goods Goodsc =new Goods();
									Integer zoneNum=goodsS.getZoneNum()+1;
									Goodsc.setZoneNum(zoneNum);
									Goodsc.setGoodsId(goodsId+"");
									resut=goodsService.updateGoodsNum(Goodsc);
								}
					    	}
					    }
			    	}
		    	}
		    }
		    if("zk".equals(user.getCategoryCode())){
		    	//知客存储，赏金和结束时间
		    	topic.setReward(reward);		    	
		        topic.setEndDate(DateUtils.getAfterdayUnixTime(7));
		        //赏金不足时，不可以发布
		        Double curJe= accountInfoService.selectCurJeByUserId(createUser);
		        if(topic.getReward() > 0 && topic.getReward() > curJe){
		        	statusCodeEnum = StatusCodeEnum.CODE300301;
		        	entity.setCode(statusCodeEnum.getCode());
		    		entity.setMsg(statusCodeEnum.getMsg());
		    		return entity;
		        }else{
		        	if(topic.getReward() > 0){
		        		//发布赏金话题，占用余额
			        	boolean backUseJe = accountInfoService.mainUseJeToUse(createUser, topic.getReward(),"打赏话题");
			        	if(backUseJe == true){
			        		resut= topicService.insertTopicAndKnow(topic);
			        	}
		        	}else{
		        		resut= topicService.insertTopicAndKnow(topic);
		        	}
		        }
		    }                
		    long tid=topic.getTopicId();
		    boolean bool = false;
	        if(resut > 0 && resut!=null){          
			    Integer resuts=  topicPictureService.insertTopicPicture(tid, pictures);
			    if(resuts > 0 && resuts!=null){
				    //增加 用户 文章 发表数
				    bool = userService.updateUserTopicNum(topic.getCreateUser());
			    }
	        }
	        if(bool==true){
	        	statusCodeEnum = StatusCodeEnum.CODE000000;
	//        	entity.setData(resut);
	        }else{
	        	statusCodeEnum = StatusCodeEnum.CODE100000;
	        }       
        } catch (Exception e) {
        	log.error(e.getMessage());
        	statusCodeEnum = StatusCodeEnum.CODE100000;
		}
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
    }
	/**
	 * 收藏
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value="/collect",method=RequestMethod.POST)  
    public ReturnJsonEntity collectWhether(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum = StatusCodeEnum.CODE100000;
		
		try{
			//游客身份验证
			if(String.valueOf(getTokenUserId()).equals(guessUserId)){
				statusCodeEnum=StatusCodeEnum.CODE100200;
				entity.setCode(statusCodeEnum.getCode());
				entity.setMsg(statusCodeEnum.getMsg());
				return entity;
			}
			//获取当前时间，转为INT存储
	        Integer timeDate =DateUtils.getUnixTimeByDate(DateUtils.getNow());
	        long topicId = getLongParam("topicId");
	        long userId= getTokenUserId();
	        TopicCollect topicCollects =new TopicCollect();
	        topicCollects.setCreateDate(timeDate);
	        topicCollects.setUserId(userId);
	        topicCollects.setTopicId(topicId);
		    
		    Integer resut = topicCollectService.collectWhether(topicCollects);
	
	        if(resut > 0){
	        	statusCodeEnum = StatusCodeEnum.CODE000000;
	//        	entity.setData(resut);
	        }else{
	        	statusCodeEnum = StatusCodeEnum.CODE100000;
	        }
		} catch (Exception e) {
			log.error(e.getMessage());
			statusCodeEnum=StatusCodeEnum.CODE100000;
		}
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
    }
	/**
	 * 点赞
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value="/praise",method=RequestMethod.POST)  
    public ReturnJsonEntity praiseWhether(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum = StatusCodeEnum.CODE100000;
		try{
			//获取当前时间，转为INT存储
	        Integer timeDate =DateUtils.getUnixTimeByDate(DateUtils.getNow());
	        long topicId = getLongParam("topicId");
	        long userId= getTokenUserId();
	        TopicPraise TopicPraises =new TopicPraise();
	        TopicPraises.setCreateDate(timeDate);
	        TopicPraises.setUserId(userId);
	        TopicPraises.setTopicId(topicId);
		    
		    Integer resut = topicPraiseService.praiseWhether(TopicPraises);
	
	        if(resut > 0){
	        	statusCodeEnum = StatusCodeEnum.CODE000000;
	//        	entity.setData(resut);
	        }else{
	        	statusCodeEnum = StatusCodeEnum.CODE100000;
	        }
		} catch (Exception e) {
			log.error(e.getMessage());
			statusCodeEnum=StatusCodeEnum.CODE100000;
		}
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
    }
	
	/**
	 * 打赏
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value="/rewardgave",method=RequestMethod.POST)  
    public ReturnJsonEntity gaveReward(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum = StatusCodeEnum.CODE100000;
        Long topicId = getLongParam("topicId");
        Long commentId = getLongParam("commentId");
        //游客身份验证
  		if(String.valueOf(getTokenUserId()).equals(guessUserId)){
  			statusCodeEnum=StatusCodeEnum.CODE100200;
  			entity.setCode(statusCodeEnum.getCode());
  			entity.setMsg(statusCodeEnum.getMsg());
  			return entity;
  		}
        try {
	        boolean bResut=topicService.topicReward(topicId, commentId);
	        
	        if(bResut){
	        	statusCodeEnum = StatusCodeEnum.CODE000000;
	        }else{
	        	statusCodeEnum = StatusCodeEnum.CODE100000;
	        }
        } catch (Exception e) {
        	log.error(e.getMessage());
        	statusCodeEnum = StatusCodeEnum.CODE100000;
		}
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
    }
	/**
	 * 查看当前用户所有话题列表
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value="/mylaunchyopic",method=RequestMethod.GET)  
    public ReturnJsonEntity topicPageList(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum=StatusCodeEnum.CODE100000;
		try{
	        Integer pageNow = getIntParam("pageNow");
	//        long userId=getLongParam("userId");
	        long createUser= getTokenUserId();//创建人ID
	 
		    List<Topic> topicList = topicService.userIdTopicListPage(createUser, pageNow);
	
		    entity.setData(topicList);
		    statusCodeEnum = StatusCodeEnum.CODE000000;
		} catch (Exception e) {
			log.error(e.getMessage());
			statusCodeEnum=StatusCodeEnum.CODE100000;
			entity.setData(new Array());
		}
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
    }
	/**
	 * 查看用户所有话题 话题分页
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value="/userIdTopicpage",method=RequestMethod.GET)  
    public ReturnJsonEntity myLaunchyTopic(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum=StatusCodeEnum.CODE100000;
		try{
			Integer pageNow = getIntParam("pageNow");
			long userId = getLongParam("userId");// 创建人ID
			String key = getParam("key");
			long curUserId = getTokenUserId();
			if (userId == 0) {
				userId = curUserId;
			}
			if (pageNow==0) {
				pageNow=1;
			}
			PageUtil_TopicCollect pageList = topicService.userIdTopicPageList(curUserId,userId, pageNow,key);
	
	        entity.setData(pageList);
	        statusCodeEnum = StatusCodeEnum.CODE000000;
		} catch (Exception e) {
			log.error(e.getMessage());
			statusCodeEnum=StatusCodeEnum.CODE100000;
			entity.setData(new Array());
		}
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
    }
	/**
	 * 话题详情，查看单个话题,评论
	 * @param request
	 * @param model
	 * @return
	 */
    @RequestMapping(value="/detail",method=RequestMethod.GET)  
    public ReturnJsonEntity topicDetail(){
    	ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum = StatusCodeEnum.CODE100000;
		try{
			
			long topicId = getLongParam("topicId");
	        long userId= getTokenUserId();
	       
	        com.zimmur.restful.model.Topic topic = topicService.selectByTopicIdV120(topicId,userId);
	        
	        if(topic != null){
	        	entity.setData(topic);
	        	statusCodeEnum = StatusCodeEnum.CODE000000;
	        }else{
	        	entity.setData(new ArrayList<>());
	        	statusCodeEnum = StatusCodeEnum.CODE300404;
	        }
		} catch (Exception e) {
			log.error(e.getMessage());
			entity.setData(new ArrayList<>());
			statusCodeEnum=StatusCodeEnum.CODE100000;
		}
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
    }	
	/**
	 * 评论录入  
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value="/addcomment",method=RequestMethod.POST)  
    public ReturnJsonEntity addcomment(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum = StatusCodeEnum.CODE100000;
		try{
			//游客身份验证
			if(String.valueOf(getTokenUserId()).equals(guessUserId)){
				statusCodeEnum=StatusCodeEnum.CODE100200;
				entity.setCode(statusCodeEnum.getCode());
				entity.setMsg(statusCodeEnum.getMsg());
				return entity;
			}
			
	        long commentId =IDWorkFactory.getID(AppModuleEnum.Zone);
	        long topicId = getLongParam("topicId");
			String content = getParam("content");
			//String commentType=getParam("type");
			long userCommentId = getLongParam("commentId");
			//获取当前时间，转为INT存储
	        Integer createDate =DateUtils.getUnixTimeByDate(DateUtils.getNow());
	        GoodsUser createUser = userService.selectByUserId(getTokenUserId());
	        TopicComment topicComment=new TopicComment();
	        topicComment.setCommentId(commentId);
	        topicComment.setTopicId(topicId);
	        topicComment.setContent(content);
	        topicComment.setUserCommentId(userCommentId);
	        topicComment.setCreateUserId(createUser.getUserId());
	        topicComment.setCreateUserName(createUser.getNickName());
	        topicComment.setCreateCategoryName(createUser.getCategoryName());
	        topicComment.setCreateCode(createUser.getCategoryCode());
	        topicComment.setCreateDate(createDate);
	        topicComment.setCommentType("text");

	        Integer resut = topicCommentService.insertTopicComment(topicComment);
	        
	        if(resut > 0){
	        	statusCodeEnum = StatusCodeEnum.CODE000000;
	        }else{
	        	statusCodeEnum = StatusCodeEnum.CODE100000;
	        }
		} catch (Exception e) {
			log.error(e.getMessage());
			statusCodeEnum=StatusCodeEnum.CODE100000;
		}
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
    }
	/**
	 * 转发
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value="/topicshare",method=RequestMethod.POST)  
    public ReturnJsonEntity topicShare(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum = StatusCodeEnum.CODE100000;
		try{
//        	String share = getParam("share");
			long topicId = getLongParam("topicId");
			long createUser= getTokenUserId();
			
	        Integer resut = topicService.updateStopicShareNum(topicId,createUser);
	        
	        if(resut > 0){
	        	statusCodeEnum = StatusCodeEnum.CODE000000;
	//        	entity.setData(resut);
	        }else{
	        	statusCodeEnum = StatusCodeEnum.CODE100000;
	        }
		} catch (Exception e) {
			log.error(e.getMessage());
			statusCodeEnum=StatusCodeEnum.CODE100000;
		}
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
    }
	/**
	 * 关注的社区用户首页
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value="/concernUserIndex",method=RequestMethod.GET)  
    public ReturnJsonEntity concerndesignerIndex(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum=StatusCodeEnum.CODE100000;
		//游客身份验证
		if(String.valueOf(getTokenUserId()).equals(guessUserId)){
			statusCodeEnum=StatusCodeEnum.CODE100200;
			entity.setCode(statusCodeEnum.getCode());
			entity.setMsg(statusCodeEnum.getMsg());
			return entity;
		}
		try {
			long createUserId= getTokenUserId();
			
			ConcernUserView concernUserView = topicFavService.selectConcernUserIndex(createUserId);
			entity.setData(concernUserView);  
			statusCodeEnum = StatusCodeEnum.CODE000000;
		} catch (Exception e) {
			log.error(e.getMessage());
			statusCodeEnum=StatusCodeEnum.CODE100000;
			entity.setData(new Array());
		}    	  
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
    }
	/**
	 * 关注的社区用户  分页
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value="/concernUserPage",method=RequestMethod.GET)  
    public ReturnJsonEntity concernUserPage(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum=StatusCodeEnum.CODE100000;
		//游客身份验证
		if(String.valueOf(getTokenUserId()).equals(guessUserId)){
			statusCodeEnum=StatusCodeEnum.CODE100200;
			entity.setCode(statusCodeEnum.getCode());
			entity.setMsg(statusCodeEnum.getMsg());
			return entity;
		}
		try {
			Integer pageNow = getIntParam("pageNow");
			Integer userType=getIntParam("userType");
			long createUserId= getTokenUserId();
			PageUtil<ConcernView> page;
			if(userType == 1 || userType == 2 || userType == 3){
				page = topicFavService.selectConcernUserByTopicFav(createUserId, pageNow, userType);
			}else{
				page=new PageUtil<>();
			}
						
			entity.setData(page);  
			statusCodeEnum = StatusCodeEnum.CODE000000;
		} catch (Exception e) {
			log.error(e.getMessage());
			statusCodeEnum=StatusCodeEnum.CODE100000;
			entity.setData(new Array());
		}    	  
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
    }
	/**
	 * 关注社区用户
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value="/concernUser",method=RequestMethod.POST)  
    public ReturnJsonEntity concernUser(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum = StatusCodeEnum.CODE100000;
		//获取当前时间，转为INT存储
        Integer timeDate =DateUtils.getUnixTimeByDate(DateUtils.getNow());
		long userId = getLongParam("userId");
		int type=getIntParam("type");
        long createUserId= getTokenUserId();
      //游客身份验证
		if(String.valueOf(createUserId).equals(guessUserId)){
			statusCodeEnum=StatusCodeEnum.CODE100200;
			entity.setCode(statusCodeEnum.getCode());
			entity.setMsg(statusCodeEnum.getMsg());
			return entity;
		}
        try {
        	if(type==1 || type==2||type==3){
            	TopicFav isExist=topicFavService.selectTopicFav(createUserId, userId);
    	        Integer FollowNum=0;
    	        Integer resut=0;
    	        if(null == isExist){
    	        	TopicFav topicFav=new TopicFav();
                	topicFav.setCreateUserId(createUserId);
                	topicFav.setCreateDate(timeDate);
                	topicFav.setUserId(userId);
                	topicFav.setUserType((short)type);
                	
    			    resut= topicFavService.insertTopicFav(topicFav);
    			    FollowNum = 1;
    	        }else{ 
    	        	isExist.setCreateDate(DateUtils.getNowUnixTime());
    	        	if(isExist.getUserType().intValue()==type){
    	        		if( isExist.getStatus()==1){
    	        			isExist.setStatus(0);
    	        			FollowNum = -1;
    	        		}else if( isExist.getStatus()==0){
    	        			isExist.setStatus(1);
    	        			FollowNum = 1;
    	        		}
    	        	}else{
    	        		if(isExist.getStatus()==0){
    	        			isExist.setStatus(1);    	        			
    	        			FollowNum = 1;
    	        		}else{
    	        			isExist.setStatus(0);    	        			
    	        			FollowNum = -1;
    	        		}
    	        		isExist.setUserType((short)type);
    	        		resut= topicFavService.updateTopicFav(isExist);
    	        	}
    	        	resut= topicFavService.updateTopicFav(isExist);
    	        }
    	        boolean bool= userService.updateUserFollowNum(userId,FollowNum);   
    	        if(resut > 0 && bool){
    	        	statusCodeEnum = StatusCodeEnum.CODE000000;
    	        }else{
    	        	statusCodeEnum = StatusCodeEnum.CODE100000;
    	        }
        	}else{
        		statusCodeEnum = StatusCodeEnum.CODE100000;
        	}
        	
        } catch (Exception e) {
			log.error(e.getMessage());
			statusCodeEnum=StatusCodeEnum.CODE100000;
		}
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
    }
	/**
	 * 我收藏的话题
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value="/mycollecttopic",method=RequestMethod.GET)  
    public ReturnJsonEntity myCollectTopic(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum=StatusCodeEnum.CODE100000;
		try{
	        Integer pageNow = getIntParam("pageNow");
			long createUser= getTokenUserId();
	 
			List<Topic> goodsUserList = topicService.myCollectTopicList(createUser,pageNow);
	        
			entity.setData(goodsUserList);
			statusCodeEnum = StatusCodeEnum.CODE000000;
		} catch (Exception e) {
			log.error(e.getMessage());
			statusCodeEnum=StatusCodeEnum.CODE100000;
			entity.setData(new Array());
		}
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
    }
	/**
	 * 我参与的话题
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value="/myinvolvementtopic ",method=RequestMethod.GET)  
    public ReturnJsonEntity myInvolvementTopic (HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum=StatusCodeEnum.CODE100000;
		try{
	        Integer pageNow = getIntParam("pageNow");
			long createUser= getTokenUserId();
	 
			List<Topic> goodsUserList = topicService.myInvolvementTopic(createUser,pageNow);
	        
			entity.setData(goodsUserList);
			statusCodeEnum = StatusCodeEnum.CODE000000;
		} catch (Exception e) {
			log.error(e.getMessage());
			statusCodeEnum=StatusCodeEnum.CODE100000;
			entity.setData(new Array());
		}
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
    }
	/**
	 * 随机推荐人
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value="/commandUser",method=RequestMethod.GET)  
    public ReturnJsonEntity commandUser(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum=StatusCodeEnum.CODE100000;
		try{
	        Integer type=getIntParam("type");
			long createUserId= getTokenUserId();
			
			List<GoodsUser> commandUsers=topicRecommandService.selectTopicRecommandList3(type.shortValue(), createUserId);
	        
			entity.setData(commandUsers);
			statusCodeEnum = StatusCodeEnum.CODE000000;
		} catch (Exception e) {
			log.error(e.getMessage());
			statusCodeEnum=StatusCodeEnum.CODE100000;
			entity.setData(new Array());
		}
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
    }
	/**
	 * 评论列表，分页
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value="/commentlist",method=RequestMethod.GET)  
    public ReturnJsonEntity commentList(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum=StatusCodeEnum.CODE100000;
		try{
	        Integer pageNow = getIntParam("pageNow");
	        long topicId = getLongParam("topicId");
	        long userId= getTokenUserId();
	        if (pageNow==0) {
				pageNow=1;
			}
	        PageUtil<TopicComment> pageUtil=new PageUtil<>();
		    
		    int totalRecord =topicCommentService.countByTopicIdTopicComment(topicId);
		    if (totalRecord>0) {
		    	List<TopicComment> topicCommentList = topicCommentService.pageCommentList(topicId,userId, pageNow);
		    	if(topicCommentList!=null && topicCommentList.size()>0){
		    		pageUtil.setItemList(topicCommentList);
		    	}
			}
		    
		    pageUtil.setPageNo(pageNow);
		    pageUtil.setTotalRecord(totalRecord);
		    entity.setData(pageUtil);
		    statusCodeEnum = StatusCodeEnum.CODE000000;
		} catch (Exception e) {
			log.error(e.getMessage());
			statusCodeEnum=StatusCodeEnum.CODE100000;
			entity.setData(new Array());
		}
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
    }
	
}  